篇首语:本文由编程笔记#小编为大家整理,主要介绍了ffmpeg调用directshow camera 并sdl渲染相关的知识,希望对你有一定的参考价值。
ffmpeg使用设备先注册
avdevice_register_all();
使用常用得CCameraDS 取获取名称,这里使用第一个摄像头。这里我们使用SDL来渲染得时候,启动一个定时器来定时给一个事件让SDL来刷新
SDLSDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
所以使用video和timer来完成这种渲染。
这里使用bgr24来渲染,使用sdl 创建表面,指定opengl来做
1 创建窗口
2 创建刷新线程
3 线程中发送更新事件
4 main主循环捕获事件,获取一帧rgb24,刷新界面渲染
这里面可以修改变成这样
刷新线程渲染获取一帧,推给主线程,主线程负责编码发送,同时渲染,这里是示例,没有这么做。
int main()
avdevice_register_all();
int w = 1280;
int h = 720;
g_fps = 10;
std::wstring cname;
CCameraDS::CameraName(0, cname);
std::string name = UnicodeToUTF8(cname);
TFFCap cap;
int ret = cap.OpenCameraRGB(name.c_str(), 1280, 720);
if (ret != 0)
return -1;
int screen_w, screen_h;
SDL_Window *screen;
SDL_Renderer *sdlRenderer;
SDL_Texture *sdlTexture;
SDL_Rect sdlRect;
SDL_Thread *video_tid;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
screen_w = w; //w;
screen_h = h;
//screen_h = screen_w * 9 / 16;
screen = SDL_CreateWindow("FF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
screen_w, screen_h, SDL_WINDOW_OPENGL);
if (!screen)
std::cout << "SDL: could not create window - exiting: " << SDL_GetError() << std::endl;
return -1;
sdlRenderer &#61; SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
sdlTexture &#61; SDL_CreateTexture(sdlRenderer,
SDL_PIXELFORMAT_BGR24 , //SDL_PIXELFORMAT_IYUV, //SDL_PIXELFORMAT_BGR24
SDL_TEXTUREACCESS_STREAMING,/*SDL_TEXTUREACCESS_STREAMING,*/
screen_w, screen_h);
sdlRect.x &#61; 0;
sdlRect.y &#61; 0;
sdlRect.w &#61; screen_w;
sdlRect.h &#61; screen_h;
video_tid &#61; SDL_CreateThread(sfp_refresh_thread, NULL, NULL);
int got_picture;
for (;;)
SDL_WaitEvent(&event);
if (event.type &#61;&#61; SFM_REFRESH_EVENT)
uint8_t *frame &#61; cap.QueryFrame();
if (frame !&#61; NULL)
int y &#61; screen_h * 2 / 3;
SDL_UpdateTexture(sdlTexture, NULL/*&sdlRect*/, frame, w * 3);
//SDL_UpdateTexture(sdlTexture, &sdlRect, out_buffer &#43; W * H * 3, -W * 3);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);
else if (event.type &#61;&#61; SDL_KEYDOWN)
if (event.key.keysym.sym &#61;&#61; SDLK_SPACE)
thread_pause &#61; !thread_pause;
else if (event.type &#61;&#61; SDL_QUIT)
thread_exit &#61; 1;
else if (event.type &#61;&#61; SFM_BREAK_EVENT)
break;
SDL使用得事件如下&#xff1a;
int thread_exit &#61; 0;
int thread_pause &#61; 0;
int g_fps &#61; 30;
int sfp_refresh_thread(void *opaque)
thread_exit &#61; 0;
thread_pause &#61; 0;
while (!thread_exit)
if (!thread_pause)
SDL_Event event;
event.type &#61; SFM_REFRESH_EVENT;
SDL_PushEvent(&event);
SDL_Delay(1000/g_fps);
thread_exit &#61; 0;
thread_pause &#61; 0;
//Break
SDL_Event event;
event.type &#61; SFM_BREAK_EVENT;
SDL_PushEvent(&event);
return 0;
线程里面每隔1000/g_fps得时间就产生一个渲染事件&#xff0c;取得一帧数据后渲染&#xff0c;当然这里也可以编码发送等等都可以做。事件发生等等也可以使用标准c&#43;&#43;得事件等待互斥&#xff0c;原理都是一摸一样得。
最后给出实现得FFcap
class TFFCap
public:
int m_fps &#61; 10;
AVFrame * m_pFrame &#61; NULL;
AVFrame * m_pFrameDst &#61; NULL;
string m_name;
uint8_t * out_buffer &#61; NULL;
int m_videoIndex &#61; -1;
struct SwsContext * m_img_convert_ctx &#61; NULL;
//int _pixelW;
//int _pixelH;
private:
AVFormatContext * m_pFormatCtx &#61; NULL;
AVCodecContext * m_pCodecCtx &#61; NULL;
AVCodec * m_pCodec &#61; NULL;
AVPacket * m_pkt &#61; NULL;
public:
void Init()
avdevice_register_all();
int OpenCameraRGB(const char * utf8videoname,
int nWidth, int nHeight);
uint8_t *QueryFrame();
int m_w &#61; 0;
int m_h &#61; 0;
// int QueryFrame(uint8_t *buf, int *len);
void UnInit();
TFFCap();
~TFFCap();
;
TFFCap实现
#include "FFCap.h"
#include
#include
using namespace std;
TFFCap::TFFCap()
TFFCap::~TFFCap()
static AVDictionary *GetOptions(int videosize, int &fps)
AVDictionary *options &#61; NULL;
switch (videosize)
case 1:
fps &#61; 10;
av_dict_set(&options, "video_size", "1280x720", 0);
//const char * fname &#61; av_get_pix_fmt_name((AVPixelFormat)13);
//av_dict_set(&options, "pixel_format", fname, 0);
av_dict_set_int(&options, "framerate", 10, 0);
av_dict_set_int(&options, "rtbufsize", 2764800 / 10, 0);
av_dict_set(&options, "start_time_realtime", 0, 0);
//av_dict_set(&options, "mjpeg", 0, 0);
break;
case 2:
fps &#61; 20;
av_dict_set_int(&options, "framerate", 30, 0);
av_dict_set(&options, "video_size", "640x480", 0);
av_dict_set_int(&options, "rtbufsize", 640 * 480 * 3 / 15, 0);
break;
case 3:
fps &#61; 20;
av_dict_set_int(&options, "framerate", 30, 0);
av_dict_set(&options, "video_size", "320x240", 0);
av_dict_set_int(&options, "rtbufsize", 76800 / 10, 0);
break;
case 4:
fps &#61; 20;
av_dict_set_int(&options, "framerate", 30, 0);
av_dict_set(&options, "video_size", "176x144", 0);
//av_dict_set_int(&options, "rtbufsize", 76800 / 10, 0);
break;
av_dict_set(&options, "start_time_realtime", 0, 0);
return options;
int TFFCap::OpenCameraRGB(const char * utf8videoname,
int w, int h)
av_log_set_level(AV_LOG_FATAL);
AVInputFormat *ifmt &#61; nullptr;
AVDictionary *options &#61; NULL;
string oldname &#61; m_name;
m_name &#61; "video&#61;";
m_name &#43;&#61; utf8videoname;
if (oldname.compare(m_name) !&#61; 0)
avcodec_close(m_pCodecCtx);
avcodec_free_context(&m_pCodecCtx);
m_pCodecCtx &#61; NULL;
avformat_close_input(&m_pFormatCtx);
m_pFormatCtx &#61; NULL;
else
if (m_w &#61;&#61; w && m_h &#61;&#61; h)
return 0;
avcodec_free_context(&m_pCodecCtx);
m_pCodecCtx &#61; NULL;
avformat_close_input(&m_pFormatCtx);
m_pFormatCtx &#61; NULL;
if (m_pFormatCtx &#61;&#61; NULL)
m_pFormatCtx &#61; avformat_alloc_context();
ifmt &#61; av_find_input_format("dshow");
m_pFormatCtx->flags |&#61; AVFMT_FLAG_NOBUFFER;
av_dict_set(&options, "start_time_realtime", 0, 0);
switch (w)
case 1920:
break;
case 1280:
m_fps &#61; 10;
av_dict_set(&options, "video_size", "1280x720", 0);
//av_dict_set_int(&options, "framerate", 10, 0);
av_dict_set_int(&options, "rtbufsize", 2764800 / 10, 0);
break;
case 640:
m_fps &#61; 20;
av_dict_set_int(&options, "framerate", 30, 0);
av_dict_set(&options, "video_size", "640x480", 0);
av_dict_set_int(&options, "rtbufsize", 640 * 480 * 3 / 15, 0);
break;
case 320:
m_fps &#61; 20;
av_dict_set_int(&options, "framerate", 30, 0);
av_dict_set(&options, "video_size", "320x240", 0);
av_dict_set_int(&options, "rtbufsize", 76800 / 10, 0);
break;
case 176:
m_fps &#61; 20;
av_dict_set_int(&options, "framerate", 30, 0);
av_dict_set(&options, "video_size", "176x144", 0);
av_dict_set_int(&options, "rtbufsize", 76800 / 10, 0);
break;
if (avformat_open_input(&m_pFormatCtx, m_name.c_str(), ifmt, &options) !&#61; 0)
return -1;
if (avformat_find_stream_info(m_pFormatCtx, NULL) < 0)
//std::cout <<"avformat find stream info failed." <
return -1;
for (uint32_t i &#61; 0; i < m_pFormatCtx->nb_streams; i&#43;&#43;)
if (m_pFormatCtx->streams[i]->codecpar->codec_type &#61;&#61; AVMEDIA_TYPE_VIDEO)
//AVPixelFormat avf &#61; (AVPixelFormat)m_pFormatCtx->streams[i]->codecpar->format;
//const char* strfmt &#61; av_get_pix_fmt_name(avf);
//w &#61; m_pFormatCtx->streams[i]->codecpar->width;
//h &#61; m_pFormatCtx->streams[i]->codecpar->height;
m_videoIndex &#61; i;
break;
if (-1 &#61;&#61; m_videoIndex)
return -1;
if(m_pCodecCtx &#61;&#61; NULL)
m_pCodecCtx &#61; avcodec_alloc_context3(NULL);
if (m_pCodecCtx &#61;&#61; NULL)
return -1;
avcodec_parameters_to_context(m_pCodecCtx, m_pFormatCtx->streams[m_videoIndex]->codecpar);
m_pCodec &#61; avcodec_find_decoder(m_pCodecCtx->codec_id);
if (m_pCodec &#61;&#61; NULL)
//std::cout <<"AVCodec not found." <
return -1;
if (avcodec_open2(m_pCodecCtx, m_pCodec, NULL) < 0)
//std::cout <<"codec open failed." <
return -1;
if(m_pFrame &#61;&#61; NULL)
m_pFrame &#61; av_frame_alloc();
if(m_pFrameDst &#61;&#61; NULL)
m_pFrameDst &#61; av_frame_alloc();
if(m_pkt &#61;&#61; NULL)
m_pkt &#61; av_packet_alloc();
AVPixelFormat avpf &#61; AV_PIX_FMT_BGR24; // AV_PIX_FMT_YUV420P; //AV_PIX_FMT_BGR24
if (m_w !&#61; w || m_h !&#61; h)
if (out_buffer !&#61; NULL)
delete []out_buffer;
if (out_buffer &#61;&#61; NULL)
out_buffer &#61; (unsigned char *)av_malloc(av_image_get_buffer_size(avpf,
w,
h,
1));
av_image_fill_arrays(m_pFrameDst->data, m_pFrameDst->linesize, out_buffer,
avpf, w, h, 1);
m_img_convert_ctx &#61;
sws_getCachedContext(m_img_convert_ctx,
w, h, m_pCodecCtx->pix_fmt,
w, h, avpf, SWS_POINT, NULL, NULL, NULL);
m_w &#61; w;
m_h &#61; h;
return 0;
uint8_t *TFFCap::QueryFrame(/*int &len*/)
if (m_pFormatCtx &#61;&#61; NULL)
return NULL;
AVPacket packet;
av_init_packet(&packet);
int got_picture &#61; 0;
if (av_read_frame(m_pFormatCtx, &packet) >&#61; 0)
int ret &#61; 0;
if (packet.stream_index &#61;&#61; m_videoIndex)
if (avcodec_send_packet(m_pCodecCtx, &packet) &#61;&#61; 0)
if (avcodec_receive_frame(m_pCodecCtx, m_pFrame) &#61;&#61; 0)
got_picture &#61; 1;
sws_scale(m_img_convert_ctx,
(const uint8_t* const*)m_pFrame->data,
m_pFrame->linesize, 0,
m_pCodecCtx->height,
m_pFrameDst->data, m_pFrameDst->linesize);
av_packet_unref(&packet);
//返回上一帧
if (got_picture)
return out_buffer;
return NULL;
void TFFCap::UnInit()
av_frame_free(&m_pFrameDst);
av_frame_free(&m_pFrame);
if(m_img_convert_ctx!&#61; nullptr)
sws_freeContext(m_img_convert_ctx);
avcodec_close(m_pCodecCtx);
avcodec_free_context(&m_pCodecCtx);
avformat_close_input(&m_pFormatCtx);
效果
代码里面还有一些是可以修改得&#xff0c;仅仅作为示例&#xff0c;代表可行